﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace GameEngine
{
    public class Sprite
    {
        // The sprite sheet being used along with it's getter and setter
        public Texture2D Texture
        {
            get { return texture; }
            set
            { 
                texture = value;
                sheet_width = texture.Width / tile_width;
                sheet_height = texture.Height / tile_height;
            }
        }
        protected Texture2D texture;

        // The sheet width/height in tiles, along with the tile width/height
        protected int sheet_width, sheet_height, tile_width, tile_height;

        // This holds our rectangles for each sprite on the sheet
        protected List<Rectangle> regions = new List<Rectangle>();

        // The first constructor, if we don't have the texture yet we can init the instance with the tile width/height
        public Sprite(int tile_width, int tile_height)
        {
            this.tile_width = tile_width;
            this.tile_height = tile_height;

            CreateRegions();
        }

        // If we already have the spritesheet as a texture we can init the instance using the texture
        public Sprite(Texture2D texture, int tile_width, int tile_height, int sheet_width = 0, int sheet_height = 0)
        {
            this.texture = texture;

            if (sheet_width == 0)
                sheet_width = texture.Width / tile_width;
            if (sheet_height == 0)
                sheet_height = texture.Height / tile_height;

            this.sheet_width = sheet_width;
            this.sheet_height = sheet_height;

            this.tile_width = tile_width;
            this.tile_height = tile_height;

            CreateRegions();
        }

        // If we havent yet got our texture we can send in our contentmanager and the asset name and load it from the constructor
        public Sprite(ContentManager Content, string assetname, int tile_width, int tile_height, int sheet_width = 0, int sheet_height = 0)
        {
            this.texture = Content.Load<Texture2D>(assetname);

            if (sheet_width == 0)
                sheet_width = texture.Width / tile_width;
            if (sheet_height == 0)
                sheet_height = texture.Height / tile_height;

            this.sheet_width = sheet_width;
            this.sheet_height = sheet_height;

            this.tile_width = tile_width;
            this.tile_height = tile_height;

            CreateRegions();
        }

        // This creates the rectangles for each sprite on our sheet
        private void CreateRegions()
        {
            for (int y = 0; y < sheet_height; y++)
            {
                for (int x = 0; x < sheet_width; x++)
                {
                    regions.Add(new Rectangle(x * tile_width, y * tile_height, tile_width, tile_height));
                }
            }
        }

        // Get a specific rectangle for a sprite on the sheet
        public Rectangle GetRegion(int i)
        {
            return regions[i];
        }

        // This disposes of the instance of this class
        public void Dispose()
        {
            this.Dispose();
            GC.SuppressFinalize(this);
        }
    }
}
